/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.orders.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.IdGen;
import com.jeeplus.common.utils.SnowFlake;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.mapper.AccountMapper;
import com.jeeplus.modules.address.entity.Address;
import com.jeeplus.modules.address.mapper.AddressMapper;
import com.jeeplus.modules.dynamicrecord.entity.DynamicRecord;
import com.jeeplus.modules.dynamicrecord.mapper.DynamicRecordMapper;
import com.jeeplus.modules.goods.entity.Goods;
import com.jeeplus.modules.goods.mapper.GoodsMapper;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.mapper.MemberMapper;
import com.jeeplus.modules.orderbalance.entity.OrderBalance;
import com.jeeplus.modules.orderbalance.mapper.OrderBalanceMapper;
import com.jeeplus.modules.teamrecord.entity.TeamRecord;
import com.jeeplus.modules.teamrecord.mapper.TeamRecordMapper;
import com.jeeplus.modules.teamservice.entity.TeamService;
import com.jeeplus.modules.teamservice.mapper.TeamServiceMapper;
import com.jeeplus.modules.teamservice.service.TeamServiceService;
import com.jeeplus.modules.trade.entity.Trade;
import com.jeeplus.modules.trade.mapper.TradeMapper;
import com.jeeplus.modules.trades.entity.Trades;
import com.jeeplus.modules.trades.mapper.TradesMapper;
import com.jeeplus.modules.userbank.entity.Userbank;
import com.jeeplus.modules.userbank.mapper.UserbankMapper;
import com.jeeplus.modules.userpay.entity.Userpay;
import com.jeeplus.modules.userpay.mapper.UserpayMapper;
import com.jeeplus.modules.userwx.entity.Userwx;
import com.jeeplus.modules.userwx.mapper.UserwxMapper;
import com.jeeplus.modules.yuyue.entity.Yuyue;
import com.jeeplus.modules.yuyue.service.YuyueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.orders.entity.Orders;
import com.jeeplus.modules.orders.mapper.OrdersMapper;
import redis.clients.jedis.Jedis;

/**
 * 交易订单Service
 * @author freedom
 * @version 2020-09-26
 */
@Service
@Transactional(readOnly = true)
public class OrdersService extends CrudService<OrdersMapper, Orders> {
	@Autowired
	private OrdersMapper ordersMapper;
	@Autowired
	private MemberMapper memberMapper;
	@Autowired
	private AccountMapper accountMapper;
	@Autowired
    private DynamicRecordMapper dynamicRecordMapper;
	@Autowired
	private AddressMapper addressMapper;
	@Autowired
	private TeamServiceMapper teamServiceMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private UserpayMapper userpayMapper;
	@Autowired
	private UserwxMapper userwxMapper;
	@Autowired
	private UserbankMapper userbankMapper;
	@Autowired
	private TradeMapper tradeMapper;
	@Autowired
	private TradesMapper tradesMapper;
	@Autowired
	private TeamRecordMapper teamRecordMapper;
	@Autowired
	private OrderBalanceMapper orderBalanceMapper;
	@Autowired
	private YuyueService yuyueService;
	@Autowired
	private TeamServiceService teamServiceService;

	public Orders get(String id) {
		return super.get(id);
	}
	
	public List<Orders> findList(Orders orders) {
		return super.findList(orders);
	}
	
	public Page<Orders> findPage(Page<Orders> page, Orders orders) {
		dataRuleFilter(orders);
		orders.setPage(page);
		List<Orders> ordersList = mapper.findNotFinishList(orders);
		for (Orders orders1 : ordersList) {
			Goods goods = goodsMapper.get(orders1.getGoods().getId());
			orders1.setGoodsType(goods.getGoodsType());
			orders1.setGoodPrice(goods.getPrice());
			orders1.setGoodsPic(goods.getMainImgs());
		}
		page.setList(ordersList);
		return page;
	}

	/**
	 * 正在交易
	 * @param ordersPage
	 * @param orders
	 * @return
	 */
	public Page<Orders> findTradingPage(Page<Orders> ordersPage, Orders orders) {
		dataRuleFilter(orders);
		orders.setPage(ordersPage);
		List<Orders> ordersList = mapper.findTradingList(orders);
		for (Orders orders1 : ordersList) {
			Goods goods = goodsMapper.get(orders1.getGoods().getId());
			orders1.setGoodsType(goods.getGoodsType());
			orders1.setGoodPrice(goods.getPrice());
			orders1.setGoodsPic(goods.getMainImgs());
			Trade trade = new Trade();
			trade.setOrderNo(orders1.getOrdersNo());
			List<Trade> tradeList = tradeMapper.findList(trade);
			if (tradeList.size()>0){
				orders1.setPlaceDate(DateUtils.formatDateTime(tradeList.get(0).getPlaceDate()));
			}
		}
		ordersPage.setList(ordersList);
		return ordersPage;
	}

	/**
	 * 交易成功
	 * @param ordersPage
	 * @param orders
	 * @return
	 */
	public Page<Orders> findSuccessPage(Page<Orders> ordersPage, Orders orders) {
		dataRuleFilter(orders);
		orders.setPage(ordersPage);
		List<Orders> ordersList = mapper.findSuccessList(orders);
		for (Orders orders1 : ordersList) {
			Goods goods = goodsMapper.get(orders1.getGoods().getId());
			orders1.setGoodsType(goods.getGoodsType());
			orders1.setGoodPrice(goods.getPrice());
			orders1.setGoodsPic(goods.getMainImgs());
			Trade trade = new Trade();
			trade.setOrderNo(orders1.getOrdersNo());
			List<Trade> tradeList = tradeMapper.findList(trade);
			if (tradeList.size()>0){
				orders1.setPlaceDate(DateUtils.formatDateTime(tradeList.get(0).getPlaceDate()));
			}
		}
		ordersPage.setList(ordersList);
		return ordersPage;
	}
	
	@Transactional(readOnly = false)
	public void save(Orders orders) {
		super.save(orders);
	}
	
	@Transactional(readOnly = false)
	public void delete(Orders orders) {
		super.delete(orders);
	}

	public List<Orders> findTodayList(Orders orders) {
		List<Orders> list = mapper.findTodayList(orders);
		return list;
	}

	public Page<Orders> findTodayListPage(Page<Orders> page, Orders orders) {
		orders.setPage(page);
		List<Orders> list = findTodayList(orders);
		page.setList(list);
		return page;
	}


	/**
	 * 处理订单完成收款逻辑
	 * @param member  出售人
	 * @param ordersNo  出售订单编号
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public AjaxJson manageOrders(Member member, String ordersNo) {
		AjaxJson aj = new AjaxJson();
		BigDecimal money = BigDecimal.valueOf(0);
		//购买者
        Member mem = new Member();
		Orders orders = getOrdersByOrdersNo(ordersNo);

		//购买金额
		money = money.add(BigDecimal.valueOf(orders.getMoney()));
		//利润金额
//		Goods goods1 = goodsMapper.get(orders.getGoods().getId());
//		BigDecimal proMoney = money.multiply(BigDecimal.valueOf(0.1));
//		if ("1".equals(goods1.getGoodsType())){
//			proMoney = money.multiply(BigDecimal.valueOf(0.1));
//		}
//		if ("2".equals(goods1.getGoodsType())){
//			proMoney = money.multiply(BigDecimal.valueOf(0.2));
//		}

		//购买者
		mem = memberMapper.get(orders.getTomem().getId());
		//1-16   自己也添加业绩
		Account myAccount = accountMapper.get(mem.getAccount().getId());
		myAccount.setTeam(BigDecimal.valueOf(myAccount.getTeam()).add(money).doubleValue());
		accountMapper.update(myAccount);
		
		if(orders!=null) {
			//修改出售订单订单状态    3 交易完成
			orders.setOrdersStatus("3");
			save(orders);

			//商品状态改为  4  交易完成
			Goods goods = goodsMapper.get(orders.getGoods().getId());
			goods.setGoodsStatus("4");
			//todo 持有人
			goods.setHostUser(mem.getMobile());
			goods.preUpdate();
			goodsMapper.update(goods);
			//更新交易记录
			Trade trade = new Trade();
			trade.setOrderNo(ordersNo);
			List<Trade> tradeList = tradeMapper.findList(trade);
			if (tradeList.size()>0){
				tradeList.get(0).setTradeStatus("3");
				tradeList.get(0).setTradeDate(new Date());
				tradeList.get(0).preUpdate();
				tradeMapper.update(tradeList.get(0));
			}
			//更新交易记录
			Trades trades = new Trades();
			trades.setOrderNo(ordersNo);
			List<Trades> tradesList = tradesMapper.findList(trades);
			if (tradesList.size()>0){
				tradesList.get(0).setTradeStatus("3");
				tradesList.get(0).setTradeDate(new Date());
				tradesList.get(0).preUpdate();
				tradesMapper.update(tradesList.get(0));
			}

				/*
				 * 收益处理
				 */
				//根据购买订单处理   上级团队业绩与团队人数
				if (mem.getP() != null){
					Member upMember = memberMapper.get(mem.getP().getId());
					while (upMember != null){
						Account account = accountMapper.get(upMember.getAccount().getId());
						account.setNum(account.getNum()+1);
						accountMapper.update(account);
						if (upMember.getP() != null){
							upMember = memberMapper.get(upMember.getP().getId());
						}else {
							break;
						}
					}
				}


//			int one=0;//一级代理发放次数
//			int two=0;//二级代理发放次数
//			int three=0;//三级代理发放次数
//			int four=0;//四级代理发放次数
//			Double m=0.0;//发放金额
//			Double sum=0.0;//本次发放总金额
//			String name="";
//				/*
//				 * 收益处理
//				 */
//			//根据购买订单处理   上级团队业绩与团队人数
//			if (mem.getP() != null){
//				Member upMember = memberMapper.get(mem.getP().getId());
//				while (upMember != null){
//					Account account = accountMapper.get(upMember.getAccount().getId());
//					account.setNum(account.getNum()+1);
//					//代理奖发放
//					if (account.getTeam()>=200000&&account.getTeam()<500000){
//						if (one==0&&upMember.getNum()>=1){
//							name="白银代理奖";
//							m=money.doubleValue()*0.001;
//							if (sum<m){
//								m=m-sum;
//							}else {
//								m=0.0;
//							}
//							account.setYue(account.getYue()+m);
//							sum=sum+m;
//							one=one+1;
//						}
//					}else if (account.getTeam()>=500000&&account.getTeam()<1000000){
//						if (two==0&&upMember.getNum()>=2){
//
//						}
//						name="黄金代理奖";
//						m=money.doubleValue()*0.002;
//						if (sum<m){
//							m=m-sum;
//						}else {
//							m=0.0;
//						}
//						account.setYue(account.getYue()+m);
//						sum=sum+m;
//						two=two+1;
//					}else if (account.getTeam()>=1000000&&account.getTeam()<3000000){
//						if (three==0&&upMember.getNum()>=3){
//							name="白金代理奖";
//							m=money.doubleValue()*0.003;
//							if (sum<m){
//								m=m-sum;
//							}else {
//								m=0.0;
//							}
//							account.setYue(account.getYue()+m);
//							sum=sum+m;
//							three=three+1;
//						}
//					}else if (account.getTeam()>=3000000&&four==0){
//						if (upMember.getNum()>=4){
//							name="钻石代理奖";
//							m=money.doubleValue()*0.004;
//							if (sum<m){
//								m=m-sum;
//							}else {
//								m=0.0;
//							}
//							account.setYue(account.getYue()+m);
//							sum=sum+m;
//							four=four+1;
//						}
//					}
//					if (m>0){
//						//插入收益记录
//						DynamicRecord dynamicRecord = new DynamicRecord();
//						dynamicRecord.setId(IdGen.uuid());
//						dynamicRecord.preInsert();
//						dynamicRecord.setFrommem(mem);
//						dynamicRecord.setMember(upMember);
//						dynamicRecord.setMoney(m);
//						dynamicRecord.setName(name);
//						dynamicRecordMapper.insert(dynamicRecord);
//					}
//					accountMapper.update(account);
//					if (upMember.getP() != null){
//						upMember = memberMapper.get(upMember.getP().getId());
//					}else {
//						break;
//					}
//				}
//			}

		        //团队业绩
				if (mem.getP() != null){
					Member pMember = memberMapper.get(mem.getP().getId());
					if (pMember != null){
						while (pMember != null){
							Account account = accountMapper.get(pMember.getAccount().getId());
							account.setTeam(BigDecimal.valueOf(account.getTeam()).add(money).doubleValue());
							accountMapper.update(account);
							if (pMember.getP() != null){
								pMember = memberMapper.get(pMember.getP().getId());
							}else {
								break;
							}
						}
					}
				}
				//购买者团队业绩增加记录
				TeamRecord teamRecord = new TeamRecord();
				teamRecord.setId(IdGen.uuid());
				teamRecord.setMember(mem);
				teamRecord.setMoney(money.doubleValue());
				teamRecord.setIsNewRecord(true);
				teamRecord.preInsert();
				teamRecordMapper.insert(teamRecord);




			//查询手续费
			TeamService teamService = new TeamService();
			teamService.setGoods(goods);
			teamService.setMember(member);
			List<TeamService> teamServiceList = teamServiceService.findList(teamService);

			//团队手续费
			if (mem.getP() != null){
				Member pMember = memberMapper.get(mem.getP().getId());
				if (pMember != null){
					while (pMember != null){
						Account account = accountMapper.get(pMember.getAccount().getId());
						if (teamServiceList.size()>0){
							account.setTeamService(account.getTeamService()+teamServiceList.get(0).getMoney());
							accountMapper.update(account);
						}

						if (pMember.getP() != null){
							pMember = memberMapper.get(pMember.getP().getId());
						}else {
							break;
						}
					}
				}
			}
			//购买者团队手续费增加记录
//			TeamService teamService = new TeamService();
//			teamService.setId(IdGen.uuid());
//			teamService.setMember(mem);
//			teamService.setMoney(money.doubleValue());
//			teamServiceMapper.insert(teamService);






			if (mem.getP() != null){
				Member upMem = memberMapper.get(mem.getP().getId());
				if (upMem != null){
					//处理账户收益明细及积分

					BigDecimal recordMoney = money.multiply(BigDecimal.valueOf(0.005));
//		                BigDecimal integral = money.multiply(BigDecimal.valueOf(0.08)).multiply(BigDecimal.valueOf(0.02));
					Account account = accountMapper.get(upMem.getAccount().getId());
					if (account.getFrozenMoney() != null && account.getFrozenMoney() != 0){
						BigDecimal mm = BigDecimal.valueOf(account.getYue());
						BigDecimal m = BigDecimal.valueOf(account.getFrozenMoney());
						if (recordMoney.compareTo(m) == 1 ){
							//冻结余额
							account.setFrozenMoney(BigDecimal.valueOf(0).doubleValue());
							//余额
							account.setYue(recordMoney.add(m).add(BigDecimal.valueOf(account.getYue())).doubleValue());
							//记录明细
							OrderBalance orderBalance = new OrderBalance();
							orderBalance.setId(IdGen.uuid());
							orderBalance.setUpBalance(mm.doubleValue());
							orderBalance.setMember(account.getMember());
							orderBalance.setMoney(m.doubleValue());
							orderBalance.setBackBalance(m.doubleValue());
							orderBalance.setBalanceFlag("4");
							orderBalance.setIsNewRecord(true);
							orderBalance.preInsert();
							orderBalanceMapper.insert(orderBalance);
						}else {
							//冻结余额
							account.setFrozenMoney(BigDecimal.valueOf(account.getFrozenMoney()).subtract(recordMoney).doubleValue());
							//余额
							account.setYue(recordMoney.multiply(BigDecimal.valueOf(2)).add(BigDecimal.valueOf(account.getYue())).doubleValue());
							//记录明细
							OrderBalance orderBalance = new OrderBalance();
							orderBalance.setId(IdGen.uuid());
							orderBalance.setUpBalance(mm.doubleValue());
							orderBalance.setMember(account.getMember());
							orderBalance.setMoney(m.doubleValue());
							orderBalance.setBackBalance(recordMoney.doubleValue());
							orderBalance.setBalanceFlag("4");
							orderBalance.setIsNewRecord(true);
							orderBalance.preInsert();
							orderBalanceMapper.insert(orderBalance);
						}

					}else {
						//余额
						account.setYue(recordMoney.add(BigDecimal.valueOf(account.getYue())).doubleValue());
					}

					//dynamicProfit   总动态收益
					account.setDynamicProfit(recordMoney.add(BigDecimal.valueOf(account.getDynamicProfit())).doubleValue());
					//todayDynamic    今日动态收益
					account.setTodayDynamic(recordMoney.add(BigDecimal.valueOf(account.getTodayDynamic())).doubleValue());
					//totalProfit     总收益
					account.setTotalProfit(recordMoney.add(BigDecimal.valueOf(account.getTotalProfit())).doubleValue());
					//todayProfit     今日总收益
					account.setTodayProfit(recordMoney.add(BigDecimal.valueOf(account.getTodayProfit())).doubleValue());
					//integral   积分
//		                account.setIntegral(integral.add(BigDecimal.valueOf(account.getIntegral())).doubleValue());
					accountMapper.update(account);
					//插入收益记录
					DynamicRecord dynamicRecord = new DynamicRecord();
					dynamicRecord.setId(IdGen.uuid());
					dynamicRecord.preInsert();
					dynamicRecord.setFrommem(mem);
					dynamicRecord.setMember(upMem);
					dynamicRecord.setMoney(recordMoney.doubleValue());
					dynamicRecord.setName("推荐奖1");
					dynamicRecordMapper.insert(dynamicRecord);

					if (upMem.getP() != null){
						upMem = memberMapper.get(upMem.getP().getId());
						if (upMem != null){
							//处理账户收益明细及积分
							BigDecimal recordMoney1 = money.multiply(BigDecimal.valueOf(0.004));
//		                        BigDecimal integral1 = money.multiply(BigDecimal.valueOf(0.005)).multiply(BigDecimal.valueOf(0.02));
							Account account1 = accountMapper.get(upMem.getAccount().getId());
							if (account1.getFrozenMoney() != null && account1.getFrozenMoney() != 0){
								BigDecimal mm1 = BigDecimal.valueOf(account1.getYue());
								BigDecimal m1 = BigDecimal.valueOf(account1.getFrozenMoney());
								if (recordMoney1.compareTo(m1) == 1 ){
									//冻结余额
									account1.setFrozenMoney(BigDecimal.valueOf(0).doubleValue());
									//余额
									account1.setYue(recordMoney1.add(m1).add(BigDecimal.valueOf(account1.getYue())).doubleValue());
									//记录明细
									OrderBalance orderBalance1 = new OrderBalance();
									orderBalance1.setId(IdGen.uuid());
									orderBalance1.setUpBalance(mm1.doubleValue());
									orderBalance1.setMember(account1.getMember());
									orderBalance1.setMoney(m1.doubleValue());
									orderBalance1.setBackBalance(m1.doubleValue());
									orderBalance1.setBalanceFlag("4");
									orderBalance1.setIsNewRecord(true);
									orderBalance1.preInsert();
									orderBalanceMapper.insert(orderBalance1);
								}else {
									//冻结余额
									account1.setFrozenMoney(BigDecimal.valueOf(account1.getFrozenMoney()).subtract(recordMoney1).doubleValue());
									//余额
									account1.setYue(recordMoney1.multiply(BigDecimal.valueOf(2)).add(BigDecimal.valueOf(account1.getYue())).doubleValue());
									//记录明细
									OrderBalance orderBalance1 = new OrderBalance();
									orderBalance1.setId(IdGen.uuid());
									orderBalance1.setUpBalance(mm1.doubleValue());
									orderBalance1.setMember(account1.getMember());
									orderBalance1.setMoney(m1.doubleValue());
									orderBalance1.setBackBalance(recordMoney1.doubleValue());
									orderBalance1.setBalanceFlag("4");
									orderBalance1.setIsNewRecord(true);
									orderBalance1.preInsert();
									orderBalanceMapper.insert(orderBalance1);
								}

							}else {
								//余额
								account1.setYue(recordMoney1.add(BigDecimal.valueOf(account1.getYue())).doubleValue());
							}
							//dynamicProfit   总动态收益
							account1.setDynamicProfit(recordMoney1.add(BigDecimal.valueOf(account1.getDynamicProfit())).doubleValue());
							//todayDynamic    今日动态收益
							account1.setTodayDynamic(recordMoney1.add(BigDecimal.valueOf(account1.getTodayDynamic())).doubleValue());
							//totalProfit     总收益
							account1.setTotalProfit(recordMoney1.add(BigDecimal.valueOf(account1.getTotalProfit())).doubleValue());
							//todayProfit     今日总收益
							account1.setTodayProfit(recordMoney1.add(BigDecimal.valueOf(account1.getTodayProfit())).doubleValue());
							//integral   积分
//		                        account1.setIntegral(integral1.add(BigDecimal.valueOf(account1.getIntegral())).doubleValue());
							accountMapper.update(account1);
							//插入收益记录
							DynamicRecord dynamicRecord1 = new DynamicRecord();
							dynamicRecord1.setId(IdGen.uuid());
							dynamicRecord1.setFrommem(mem);
							dynamicRecord1.setMember(upMem);
							dynamicRecord1.setMoney(recordMoney1.doubleValue());
							dynamicRecord1.setName("推荐奖2");
							dynamicRecord1.preInsert();
							dynamicRecordMapper.insert(dynamicRecord1);

//		                        if (upMem.getP() != null){
//		                            upMem = memberMapper.get(upMem.getP().getId());
//		                            if (upMem != null){
//		                                //处理账户收益明细及积分
//		                                BigDecimal recordMoney2 = proMoney.multiply(BigDecimal.valueOf(0.04));
////		                                BigDecimal integral2 = money.multiply(BigDecimal.valueOf(0.003)).multiply(BigDecimal.valueOf(0.02));
//		                                Account account2 = accountMapper.get(upMem.getAccount().getId());
//		                                //余额
//		                                account2.setYue(recordMoney2.add(BigDecimal.valueOf(account2.getYue())).doubleValue());
//		                                //dynamicProfit   总动态收益
//		                                account2.setDynamicProfit(recordMoney2.add(BigDecimal.valueOf(account2.getDynamicProfit())).doubleValue());
//		                                //todayDynamic    今日动态收益
//		                                account2.setTodayDynamic(recordMoney2.add(BigDecimal.valueOf(account2.getTodayDynamic())).doubleValue());
//		                                //totalProfit     总收益
//		                                account2.setTotalProfit(recordMoney2.add(BigDecimal.valueOf(account2.getTotalProfit())).doubleValue());
//		                                //todayProfit     今日总收益
//		                                account2.setTodayProfit(recordMoney2.add(BigDecimal.valueOf(account2.getTodayProfit())).doubleValue());
//		                                //integral   积分
////		                                account2.setIntegral(integral2.add(BigDecimal.valueOf(account2.getIntegral())).doubleValue());
//		                                accountMapper.update(account2);
//		                                //插入收益记录
//		                                DynamicRecord dynamicRecord2 = new DynamicRecord();
//										dynamicRecord2.setId(IdGen.uuid());
//										dynamicRecord2.preInsert();
//		                                dynamicRecord2.setFrommem(mem);
//		                                dynamicRecord2.setMember(upMem);
//		                                dynamicRecord2.setMoney(recordMoney2.doubleValue());
//		                                dynamicRecord2.setName("推荐奖3");
//		                                dynamicRecordMapper.insert(dynamicRecord2);
//		                            }
//		                        }
						}
					}
				}
			}
			aj.setSuccess(true);
			aj.setMsg("确认收款成功");
		}else {
			aj.setSuccess(false);
			aj.setMsg("订单信息有误");
		}
		return aj;
	}


	private Orders getOrdersByOrdersNo(String ordersNo) {
		return mapper.getOrdersByOrdersNo(ordersNo);
	}

	/**
	 * 平台订单确认收款
	 * @param ids
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public void businessManage(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			BigDecimal money = BigDecimal.valueOf(0);
			//购买者
			Member mem = new Member();
			Orders orders = ordersMapper.get(id);
			//待确认
			if ("2".equals(orders.getOrdersStatus()) && orders.getMember() == null){
				//购买金额
				money = money.add(BigDecimal.valueOf(orders.getMoney()));
				//利润金额
//				Goods goods1 = goodsMapper.get(orders.getGoods().getId());
//				BigDecimal proMoney = money.multiply(BigDecimal.valueOf(0.1));
//				if ("1".equals(goods1.getGoodsType())){
//					proMoney = money.multiply(BigDecimal.valueOf(0.1));
//				}
//				if ("2".equals(goods1.getGoodsType())){
//					proMoney = money.multiply(BigDecimal.valueOf(0.2));
//				}
				//购买者
				mem = memberMapper.get(orders.getTomem().getId());
				//修改购买订单状态   1 交易完成
				orders.setOrdersStatus("3");
				ordersMapper.update(orders);
				//商品状态改为  4  交易完成
				Goods goods = goodsMapper.get(orders.getGoods().getId());
				goods.setGoodsStatus("4");
				//todo 持有人
				goods.setHostUser(mem.getMobile());
				goods.preUpdate();
				goodsMapper.update(goods);
				//更新交易记录
				Trade trade = new Trade();
				trade.setOrderNo(orders.getOrdersNo());
				List<Trade> tradeList = tradeMapper.findList(trade);
				if (tradeList.size()>0){
					tradeList.get(0).setTradeStatus("3");
					tradeList.get(0).setTradeDate(new Date());
					tradeList.get(0).preUpdate();
					tradeMapper.update(tradeList.get(0));
				}
				Trades trades = new Trades();
				trades.setOrderNo(orders.getOrdersNo());
				List<Trades> tradesList = tradesMapper.findList(trades);
				if (tradesList.size()>0){
					tradesList.get(0).setTradeStatus("3");
					tradesList.get(0).setTradeDate(new Date());
					tradesList.get(0).preUpdate();
					tradesMapper.update(tradesList.get(0));
				}
				//根据购买订单处理   上级团队业绩与团队人数
				if (mem.getP() != null){
					Member upMember = memberMapper.get(mem.getP().getId());
					while (upMember != null){
						Account account = accountMapper.get(upMember.getAccount().getId());
						account.setNum(account.getNum()+1);
						accountMapper.update(account);
						if (upMember.getP() != null){
							upMember = memberMapper.get(upMember.getP().getId());
						}else {
							break;
						}
					}
				}


//				int one=0;//一级代理发放次数
//				int two=0;//二级代理发放次数
//				int three=0;//三级代理发放次数
//				int four=0;//四级代理发放次数
//				Double m=0.0;//发放金额
//				Double sum=0.0;//本次发放总金额
//				String name="";
//				/*
//				 * 收益处理
//				 */
//				//根据购买订单处理   上级团队业绩与团队人数
//				if (mem.getP() != null){
//					Member upMember = memberMapper.get(mem.getP().getId());
//					while (upMember != null){
//						Account account = accountMapper.get(upMember.getAccount().getId());
//						account.setNum(account.getNum()+1);
//						//代理奖发放
//						if (account.getTeam()>=200000&&account.getTeam()<500000){
//							if (one==0&&upMember.getNum()>=1){
//								name="白银代理奖";
//								m=money.doubleValue()*0.001;
//								if (sum<m){
//									m=m-sum;
//								}else {
//									m=0.0;
//								}
//								account.setYue(account.getYue()+m);
//								sum=sum+m;
//								one=one+1;
//							}
//						}else if (account.getTeam()>=500000&&account.getTeam()<1000000){
//							if (two==0&&upMember.getNum()>=2){
//
//							}
//							name="黄金代理奖";
//							m=money.doubleValue()*0.002;
//							if (sum<m){
//								m=m-sum;
//							}else {
//								m=0.0;
//							}
//							account.setYue(account.getYue()+m);
//							sum=sum+m;
//							two=two+1;
//						}else if (account.getTeam()>=1000000&&account.getTeam()<3000000){
//							if (three==0&&upMember.getNum()>=3){
//								name="白金代理奖";
//								m=money.doubleValue()*0.003;
//								if (sum<m){
//									m=m-sum;
//								}else {
//									m=0.0;
//								}
//								account.setYue(account.getYue()+m);
//								sum=sum+m;
//								three=three+1;
//							}
//						}else if (account.getTeam()>=3000000&&four==0){
//							if (upMember.getNum()>=4){
//								name="钻石代理奖";
//								m=money.doubleValue()*0.004;
//								if (sum<m){
//									m=m-sum;
//								}else {
//									m=0.0;
//								}
//								account.setYue(account.getYue()+m);
//								sum=sum+m;
//								four=four+1;
//							}
//						}
//						if (m>0){
//							//插入收益记录
//							DynamicRecord dynamicRecord = new DynamicRecord();
//							dynamicRecord.setId(IdGen.uuid());
//							dynamicRecord.preInsert();
//							dynamicRecord.setFrommem(mem);
//							dynamicRecord.setMember(upMember);
//							dynamicRecord.setMoney(m);
//							dynamicRecord.setName(name);
//							dynamicRecordMapper.insert(dynamicRecord);
//						}
//						accountMapper.update(account);
//						if (upMember.getP() != null){
//							upMember = memberMapper.get(upMember.getP().getId());
//						}else {
//							break;
//						}
//					}
//				}

				//团队业绩
				if (mem.getP() != null){
					Member pMember = memberMapper.get(mem.getP().getId());
					if (pMember != null){
						while (pMember != null){
							Account account = accountMapper.get(pMember.getAccount().getId());
							account.setTeam(BigDecimal.valueOf(account.getTeam()).add(money).doubleValue());
							accountMapper.update(account);
							if (pMember.getP() != null){
								pMember = memberMapper.get(pMember.getP().getId());
							}else {
								break;
							}
						}
					}
				}
				//购买者团队业绩增加记录
				TeamRecord teamRecord = new TeamRecord();
				teamRecord.setId(IdGen.uuid());
				teamRecord.setMember(mem);
				teamRecord.setMoney(money.doubleValue());
				teamRecord.setIsNewRecord(true);
				teamRecord.preInsert();
				teamRecordMapper.insert(teamRecord);



				//查询手续费
				TeamService teamService = new TeamService();
				teamService.setGoods(goods);
				List<TeamService> teamServiceList = teamServiceService.findList(teamService);




				//团队手续费
				if (mem.getP() != null){
					Member pMember = memberMapper.get(mem.getP().getId());
					if (pMember != null){
						while (pMember != null){
							Account account = accountMapper.get(pMember.getAccount().getId());
							if (teamServiceList.size()>0){
								account.setTeamService(account.getTeamService()+teamServiceList.get(0).getMoney());
								accountMapper.update(account);
							}

							if (pMember.getP() != null){
								pMember = memberMapper.get(pMember.getP().getId());
							}else {
								break;
							}
						}
					}
				}
				//购买者团队手续费增加记录
//				TeamService teamService = new TeamService();
//				teamService.setId(IdGen.uuid());
//				teamService.setMember(mem);
//				teamService.setMoney(money.doubleValue());
//				teamServiceMapper.insert(teamService);

				if (mem.getP() != null){
					Member upMem = memberMapper.get(mem.getP().getId());
					if (upMem != null){
						//处理账户收益明细及积分
						BigDecimal recordMoney = money.multiply(BigDecimal.valueOf(0.005));
//						BigDecimal integral = money.multiply(BigDecimal.valueOf(0.007)).multiply(BigDecimal.valueOf(0.02));
						Account account = accountMapper.get(upMem.getAccount().getId());
						if (account.getFrozenMoney() != null && account.getFrozenMoney() != 0){
							BigDecimal mm = BigDecimal.valueOf(account.getYue());
							BigDecimal m = BigDecimal.valueOf(account.getFrozenMoney());
							if (recordMoney.compareTo(m) == 1 ){
								//冻结余额
								account.setFrozenMoney(BigDecimal.valueOf(0).doubleValue());
								//余额
								account.setYue(recordMoney.add(m).add(BigDecimal.valueOf(account.getYue())).doubleValue());
								//记录明细
								OrderBalance orderBalance = new OrderBalance();
								orderBalance.setId(IdGen.uuid());
								orderBalance.setUpBalance(mm.doubleValue());
								orderBalance.setMember(account.getMember());
								orderBalance.setMoney(m.doubleValue());
								orderBalance.setBackBalance(m.doubleValue());
								orderBalance.setBalanceFlag("4");
								orderBalance.setIsNewRecord(true);
								orderBalance.preInsert();
								orderBalanceMapper.insert(orderBalance);
							}else {
								//冻结余额
								account.setFrozenMoney(BigDecimal.valueOf(account.getFrozenMoney()).subtract(recordMoney).doubleValue());
								//余额
								account.setYue(recordMoney.multiply(BigDecimal.valueOf(2)).add(BigDecimal.valueOf(account.getYue())).doubleValue());
								//记录明细
								OrderBalance orderBalance = new OrderBalance();
								orderBalance.setId(IdGen.uuid());
								orderBalance.setUpBalance(mm.doubleValue());
								orderBalance.setMember(account.getMember());
								orderBalance.setMoney(m.doubleValue());
								orderBalance.setBackBalance(recordMoney.doubleValue());
								orderBalance.setBalanceFlag("4");
								orderBalance.setIsNewRecord(true);
								orderBalance.preInsert();
								orderBalanceMapper.insert(orderBalance);
							}

						}else {
							//余额
							account.setYue(recordMoney.add(BigDecimal.valueOf(account.getYue())).doubleValue());
						}
						//dynamicProfit   总动态收益
						account.setDynamicProfit(recordMoney.add(BigDecimal.valueOf(account.getDynamicProfit())).doubleValue());
						//todayDynamic    今日动态收益
						account.setTodayDynamic(recordMoney.add(BigDecimal.valueOf(account.getTodayDynamic())).doubleValue());
						//totalProfit     总收益
						account.setTotalProfit(recordMoney.add(BigDecimal.valueOf(account.getTotalProfit())).doubleValue());
						//todayProfit     今日总收益
						account.setTodayProfit(recordMoney.add(BigDecimal.valueOf(account.getTodayProfit())).doubleValue());
						//integral   积分
//						account.setIntegral(integral.add(BigDecimal.valueOf(account.getIntegral())).doubleValue());
						accountMapper.update(account);
						//插入收益记录
						DynamicRecord dynamicRecord = new DynamicRecord();
						dynamicRecord.setId(IdGen.uuid());
						dynamicRecord.preInsert();
						dynamicRecord.setFrommem(mem);
						dynamicRecord.setMember(upMem);
						dynamicRecord.setMoney(recordMoney.doubleValue());
						dynamicRecord.setName("推荐奖1");
						dynamicRecordMapper.insert(dynamicRecord);

						if (upMem.getP() != null){
							upMem = memberMapper.get(upMem.getP().getId());
							if (upMem != null){
								//处理账户收益明细及积分
								BigDecimal recordMoney1 = money.multiply(BigDecimal.valueOf(0.004));
//								BigDecimal integral1 = money.multiply(BigDecimal.valueOf(0.005)).multiply(BigDecimal.valueOf(0.02));
								Account account1 = accountMapper.get(upMem.getAccount().getId());
								if (account1.getFrozenMoney() != null && account1.getFrozenMoney() != 0){
									BigDecimal mm1 = BigDecimal.valueOf(account1.getYue());
									BigDecimal m1 = BigDecimal.valueOf(account1.getFrozenMoney());
									if (recordMoney1.compareTo(m1) == 1 ){
										//冻结余额
										account1.setFrozenMoney(BigDecimal.valueOf(0).doubleValue());
										//余额
										account1.setYue(recordMoney1.add(m1).add(BigDecimal.valueOf(account1.getYue())).doubleValue());
										//记录明细
										OrderBalance orderBalance1 = new OrderBalance();
										orderBalance1.setId(IdGen.uuid());
										orderBalance1.setUpBalance(mm1.doubleValue());
										orderBalance1.setMember(account1.getMember());
										orderBalance1.setMoney(m1.doubleValue());
										orderBalance1.setBackBalance(m1.doubleValue());
										orderBalance1.setBalanceFlag("4");
										orderBalance1.setIsNewRecord(true);
										orderBalance1.preInsert();
										orderBalanceMapper.insert(orderBalance1);
									}else {
										//冻结余额
										account1.setFrozenMoney(BigDecimal.valueOf(account1.getFrozenMoney()).subtract(recordMoney1).doubleValue());
										//余额
										account1.setYue(recordMoney1.multiply(BigDecimal.valueOf(2)).add(BigDecimal.valueOf(account1.getYue())).doubleValue());
										//记录明细
										OrderBalance orderBalance1 = new OrderBalance();
										orderBalance1.setId(IdGen.uuid());
										orderBalance1.setUpBalance(mm1.doubleValue());
										orderBalance1.setMember(account1.getMember());
										orderBalance1.setMoney(m1.doubleValue());
										orderBalance1.setBackBalance(recordMoney1.doubleValue());
										orderBalance1.setBalanceFlag("4");
										orderBalance1.setIsNewRecord(true);
										orderBalance1.preInsert();
										orderBalanceMapper.insert(orderBalance1);
									}

								}else {
									//余额
									account1.setYue(recordMoney1.add(BigDecimal.valueOf(account1.getYue())).doubleValue());
								}
								//dynamicProfit   总动态收益
								account1.setDynamicProfit(recordMoney1.add(BigDecimal.valueOf(account1.getDynamicProfit())).doubleValue());
								//todayDynamic    今日动态收益
								account1.setTodayDynamic(recordMoney1.add(BigDecimal.valueOf(account1.getTodayDynamic())).doubleValue());
								//totalProfit     总收益
								account1.setTotalProfit(recordMoney1.add(BigDecimal.valueOf(account1.getTotalProfit())).doubleValue());
								//todayProfit     今日总收益
								account1.setTodayProfit(recordMoney1.add(BigDecimal.valueOf(account1.getTodayProfit())).doubleValue());
								//integral   积分
//								account1.setIntegral(integral1.add(BigDecimal.valueOf(account1.getIntegral())).doubleValue());
								accountMapper.update(account1);
								//插入收益记录
								DynamicRecord dynamicRecord1 = new DynamicRecord();
								dynamicRecord1.setId(IdGen.uuid());
								dynamicRecord1.preInsert();
								dynamicRecord1.setFrommem(mem);
								dynamicRecord1.setMember(upMem);
								dynamicRecord1.setMoney(recordMoney1.doubleValue());
								dynamicRecord1.setName("推荐奖2");
								dynamicRecordMapper.insert(dynamicRecord1);

//								if (upMem.getP() != null){
//									upMem = memberMapper.get(upMem.getP().getId());
//									if (upMem != null){
//										//处理账户收益明细及积分
//										BigDecimal recordMoney2 = proMoney.multiply(BigDecimal.valueOf(0.04));
////										BigDecimal integral2 = money.multiply(BigDecimal.valueOf(0.003)).multiply(BigDecimal.valueOf(0.02));
//										Account account2 = accountMapper.get(upMem.getAccount().getId());
//										//余额
//										account2.setYue(recordMoney2.add(BigDecimal.valueOf(account2.getYue())).doubleValue());
//										//dynamicProfit   总动态收益
//										account2.setDynamicProfit(recordMoney2.add(BigDecimal.valueOf(account2.getDynamicProfit())).doubleValue());
//										//todayDynamic    今日动态收益
//										account2.setTodayDynamic(recordMoney2.add(BigDecimal.valueOf(account2.getTodayDynamic())).doubleValue());
//										//totalProfit     总收益
//										account2.setTotalProfit(recordMoney2.add(BigDecimal.valueOf(account2.getTotalProfit())).doubleValue());
//										//todayProfit     今日总收益
//										account2.setTodayProfit(recordMoney2.add(BigDecimal.valueOf(account2.getTodayProfit())).doubleValue());
//										//integral   积分
////										account2.setIntegral(integral2.add(BigDecimal.valueOf(account2.getIntegral())).doubleValue());
//										accountMapper.update(account2);
//										//插入收益记录
//										DynamicRecord dynamicRecord2 = new DynamicRecord();
//										dynamicRecord2.setId(IdGen.uuid());
//										dynamicRecord2.preInsert();
//										dynamicRecord2.setFrommem(mem);
//										dynamicRecord2.setMember(upMem);
//										dynamicRecord2.setMoney(recordMoney2.doubleValue());
//										dynamicRecord2.setName("推荐奖3");
//										dynamicRecordMapper.insert(dynamicRecord2);
//									}
//								}

							}
						}

					}

				}
			}
		}
	}



	/**
	 * 提交订单
	 * @param member  购买用户
	 * @param boughtGood   购买商品
	 * @return
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public AjaxJson placeOrder(Member member, Goods boughtGood) {
		AjaxJson aj = new AjaxJson();
		//避免前端页面未刷新下单优先查询商品状态
		//判断商品是否是上架中
		if (!"1".equals(boughtGood.getGoodsStatus())){
			aj.setSuccess(false);
			aj.setMsg("该商品已被购买");
			return aj;
		}
		//判断限制购买订单数
		Integer amount = member.getAmount();
		//如果添加了限制
		if (amount != null){
			Orders selectOrder = new Orders();
			selectOrder.setTomem(member);
			selectOrder.setGoods(boughtGood);
			Integer ordersAmount = ordersMapper.selectOrdersAmount(selectOrder);
			if (ordersAmount >= amount){
				aj.setSuccess(false);
				aj.setMsg("您当前购买订单过多，请等卖出后再抢购。");
				return aj;
			}
		}
		//查询当前购买商品是否是再售出
		if (StringUtils.isNotBlank(boughtGood.getOrders().getId())){
			//判断此商品出售人是否为自己
			Orders boughtOrder = get(boughtGood.getOrders().getId());
			if (member.getId().equals(boughtOrder.getMember().getId())){
				aj.setSuccess(false);
				aj.setMsg("您无法购买自己出售的商品");
				return aj;
			}
		}
		Orders entity1 = get(boughtGood.getOrders().getId());
		SnowFlake snowFlake = new SnowFlake(2, 3);
		//生成订单编号
		Long orderFormNumber = snowFlake.nextId();
		//用户的钱包
		Account account = new Account();
		account.setMember(member);
		List<Account> accountList = accountMapper.findList(account);
		if (accountList.size()>0){
			//查询用户预约记录
			Yuyue yuyue = new Yuyue();
			yuyue.setMember(member);
			yuyue.setZone(boughtGood.getGoodsType());
//			yuyue.setIsBack("0");
			yuyue.setIsOver("0");
			List<Yuyue> yuyueList = yuyueService.findList(yuyue);
			if (yuyueList.size()>0){
				//应交手续费
				BigDecimal money = BigDecimal.valueOf(boughtGood.getPrice()).multiply(BigDecimal.valueOf(0.07));
//				if ("2".equals(boughtGood.getGoodsType())){
//					money = BigDecimal.valueOf(boughtGood.getPrice()).multiply(BigDecimal.valueOf(0.1));
//				}
				//预约手续费
				BigDecimal pay = BigDecimal.valueOf(yuyueList.get(0).getMoney());
				//如果预约手续费交的多了,返还到余额
				if (pay.compareTo(money) == 1){
					accountList.get(0).setYue(pay.subtract(money).add(BigDecimal.valueOf(accountList.get(0).getYue())).doubleValue());
					accountList.get(0).preUpdate();
					accountMapper.update(accountList.get(0));
					// 返还金额记录
					yuyueList.get(0).setIsOver("1");
					yuyueList.get(0).setIsBack("1");
					yuyueList.get(0).setGiveMoney(pay.subtract(money).doubleValue());
					yuyueService.save(yuyueList.get(0));
					//插入手续费记录
					TeamService teamService = new TeamService();
					teamService.setMember(member);
					teamService.setMoney(money.doubleValue());
					teamService.setGoods(boughtGood);
					if (StringUtils.isNotBlank(boughtGood.getOrders().getId())){
						teamService.setNumber(entity1.getOrdersNo());
					}else {
						teamService.setNumber(orderFormNumber.toString());
					}

					teamService.setGoodsprice(boughtGood.getPrice());
					teamService.setGoodstype(boughtGood.getGoodsType());
					teamService.setGoodsname(boughtGood.getName());
					if ("7".equals(member.getRemarks())){
						teamService.setRemarks("7");
					}
					teamServiceService.save(teamService);
				}else {
					//预约记录出局
					yuyueList.get(0).setIsOver("1");
					yuyueService.save(yuyueList.get(0));
					//插入手续费记录
					TeamService teamService = new TeamService();
					teamService.setMember(member);
					teamService.setMoney(money.doubleValue());
					teamService.setGoods(boughtGood);
					if (StringUtils.isNotBlank(boughtGood.getOrders().getId())){
						teamService.setNumber(entity1.getOrdersNo());
					}else {
						teamService.setNumber(orderFormNumber.toString());
					}

					teamService.setGoodsprice(boughtGood.getPrice());
					teamService.setGoodstype(boughtGood.getGoodsType());
					teamService.setGoodsname(boughtGood.getName());
					if ("7".equals(member.getRemarks())){
						teamService.setRemarks("7");
					}
					teamServiceService.save(teamService);
				}
			}else {
				//未预约，则扣除未预约的手续费  5%
				BigDecimal money = BigDecimal.valueOf(boughtGood.getPrice()).multiply(BigDecimal.valueOf(0.07));
//				if ("2".equals(boughtGood.getGoodsType())){
//					money = BigDecimal.valueOf(boughtGood.getPrice()).multiply(BigDecimal.valueOf(0.1));
//				}
				//余额不足
				if (money.compareTo(BigDecimal.valueOf(accountList.get(0).getYue())) == 1){
					aj.setSuccess(false);
					aj.setMsg("余额不足,无法交付手续费");
					return aj;
				}
				//扣除手续费
				accountList.get(0).setYue(BigDecimal.valueOf(accountList.get(0).getYue()).subtract(money).doubleValue());
				accountList.get(0).preUpdate();
				accountMapper.update(accountList.get(0));
				//插入手续费记录
				TeamService teamService = new TeamService();
				teamService.setMember(member);
				teamService.setMoney(money.doubleValue());
				teamService.setGoods(boughtGood);
				if (StringUtils.isNotBlank(boughtGood.getOrders().getId())){
					teamService.setNumber(entity1.getOrdersNo());
				}else {
					teamService.setNumber(orderFormNumber.toString());
				}

				teamService.setGoodsprice(boughtGood.getPrice());
				teamService.setGoodstype(boughtGood.getGoodsType());
				teamService.setGoodsname(boughtGood.getName());
				if ("7".equals(member.getRemarks())){
					teamService.setRemarks("7");
				}
				teamServiceService.save(teamService);
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("用户钱包信息有误");
			return aj;
		}

		Orders orders = new Orders();
		//返回定单编号给前端
		String orderNo = "";
		//查询当前购买商品是否是再售出
		if (StringUtils.isNotBlank(boughtGood.getOrders().getId())){
			//是再出售，更改此订单状态为   1  等待付款
			Orders entity = get(boughtGood.getOrders().getId());
			if (entity != null){
				orderNo = entity.getOrdersNo();
				entity.setOrdersStatus("1");
				entity.setTomem(member);
				entity.setIsSale("1");
				entity.setMoney(boughtGood.getPrice());
				save(entity);
				//更新交易记录
				Trade trade = new Trade();
				trade.setGoods(boughtGood);
				trade.setMember(entity.getMember());
				List<Trade> tradeList = tradeMapper.findList(trade);
				if (tradeList.size()>0){
					tradeList.get(0).setTradeStatus("1");
					tradeList.get(0).setOrderNo(entity.getOrdersNo());
					tradeList.get(0).setPlaceDate(new Date());
					tradeList.get(0).setTomem(member);
					tradeList.get(0).preUpdate();
					tradeMapper.update(tradeList.get(0));
				}
				Trades trades = new Trades();
				trades.setGoods(boughtGood);
				trades.setMember(entity.getMember());
				List<Trades> tradesList = tradesMapper.findList(trades);
				if (tradesList.size()>0){
					tradesList.get(0).setTradeStatus("1");
					tradesList.get(0).setOrderNo(entity.getOrdersNo());
					tradesList.get(0).setPlaceDate(new Date());
					tradesList.get(0).setTomem(member);
					tradesList.get(0).preUpdate();
					tradesMapper.update(tradesList.get(0));
				}
			}
		}else {
			//首次从平台售出商品，生成订单  1 待付款
			orders.setOrdersNo(orderFormNumber.toString());
			orders.setOrdersStatus("1");
			orders.setGoods(boughtGood);
			orders.setTomem(member);
			//平台收款方式
			if (boughtGood.getPlatformCash() != null){
				if (boughtGood.getPlatformCash().getCashName().contains("银行卡")){
					orders.setPayType("2");
				}else if (boughtGood.getPlatformCash().getCashName().contains("支付宝")){
					orders.setPayType("0");
				}else if (boughtGood.getPlatformCash().getCashName().contains("微信")){
					orders.setPayType("1");
				}
			}else {
				orders.setPayType("2");
			}
			orders.setIsSale("1");
			orders.setMoney(boughtGood.getPrice());
			orders.setRemarks("平台收款");
			if ("7".equals(member.getRemarks())){
				orders.setRemarks("7");
			}
			save(orders);
			orderNo = orders.getOrdersNo();
			//更新交易记录
			Member member1 = new Member();
			member1.setLoginName("admin");
			List<Member> memberList = memberMapper.findList(member1);
			Trade trade = new Trade();
			trade.setGoods(boughtGood);
			trade.setMember(memberList.get(0));
			List<Trade> tradeList = tradeMapper.findList(trade);
			if (tradeList.size()>0){
				tradeList.get(0).setTradeStatus("1");
				tradeList.get(0).setOrderNo(orderFormNumber.toString());
				tradeList.get(0).setPlaceDate(new Date());
				tradeList.get(0).setTomem(member);
				tradeList.get(0).preUpdate();
				tradeMapper.update(tradeList.get(0));
			}
			Trades trades = new Trades();
			trades.setGoods(boughtGood);
			trades.setMember(memberList.get(0));
			List<Trades> tradesList = tradesMapper.findList(trades);
			if (tradesList.size()>0){
				tradesList.get(0).setTradeStatus("1");
				tradesList.get(0).setOrderNo(orderFormNumber.toString());
				tradesList.get(0).setPlaceDate(new Date());
				tradesList.get(0).setTomem(member);
				tradesList.get(0).preUpdate();
				tradesMapper.update(tradesList.get(0));
			}
		}
		//订单生成成功后，更改商品状态  2   等待付款
		boughtGood.setGoodsStatus("2");
		boughtGood.preUpdate();
		goodsMapper.update(boughtGood);

		aj.setSuccess(true);
		aj.setMsg("提交成功");
		aj.put("orderNo",orderNo);

		return aj;
	}


	/**
	 * 处理我已打款
	 * @param orderNo  订单编号
	 * @param paidPic   打款凭证
	 * @return
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public AjaxJson alreadyPaid(String orderNo, String paidPic) {
		AjaxJson aj = new AjaxJson();
		//根据订单编号查询订单
		Orders orders = getOrdersByOrdersNo(orderNo);
		//更新订单状态   2  待确认
		orders.setOrdersStatus("2");
		orders.setImg(paidPic);
		orders.setPaytime(new Date());
		save(orders);
		//商品状态改为  3 待确认
		Goods goods = goodsMapper.get(orders.getGoods().getId());
		goods.setGoodsStatus("3");
		goods.preUpdate();
		goodsMapper.update(goods);
		//更新交易记录
		Trade trade = new Trade();
		trade.setOrderNo(orderNo);
		List<Trade> tradeList = tradeMapper.findList(trade);
		if (tradeList.size()>0){
			tradeList.get(0).setPayDate(new Date());
			tradeList.get(0).setPayImg(paidPic);
			tradeList.get(0).setTradeStatus("2");
			tradeList.get(0).preUpdate();
			tradeList.get(0).setPrice(goods.getPrice().toString());
			tradeMapper.update(tradeList.get(0));
		}
		Trades trades = new Trades();
		trades.setOrderNo(orderNo);
		List<Trades> tradesList = tradesMapper.findList(trades);
		if (tradesList.size()>0){
			tradesList.get(0).setPayDate(new Date());
			tradesList.get(0).setPayImg(paidPic);
			tradesList.get(0).setTradeStatus("2");
			tradesList.get(0).preUpdate();
			tradesMapper.update(tradesList.get(0));
		}
		aj.setSuccess(true);
		aj.setMsg("打款成功");
		return aj;
	}


	/**
	 * 确认出售
	 * @param member  出售人
	 * @param ordersNo  订单编号
	 * @param salePrice  出售价格
	 * @param charge   手续费
	 * @param payType   收款方式
	 * @return
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public AjaxJson confirmSale(Member member, String ordersNo, String salePrice, String charge, String payType) {
		AjaxJson aj = new AjaxJson();

		//查询用户钱包
		Account account = new Account();
		account.setMember(member);
		List<Account> accountList = accountMapper.findList(account);
		if (accountList.size()<=0){
			aj.setSuccess(false);
			aj.setMsg("用户钱包信息有误");
			return aj;
		}
		//查询当前订单信息
		Orders orders = new Orders();
		orders.setOrdersNo(ordersNo);
		List<Orders> ordersList = findList(orders);
		//查询商品信息
		Goods goods = goodsMapper.get(ordersList.get(0).getGoods().getId());
		if (ordersList.size()>0){
			//系统当前时间
			Long now = System.currentTimeMillis();
			//购买完成时间
			Long boughtTime = ordersList.get(0).getUpdateDate().getTime();
			Double price=ordersList.get(0).getMoney();
			if ("0".equals(goods.getGoodsType())){
				if ((now-boughtTime)/(1000*60*60)<12){
					if (!"7".equals(member.getRemarks())){
						aj.setSuccess(false);
						aj.setMsg("请您于购买完成12小时后再出售");
						return aj;
					}
				}
			}
			if ("1".equals(goods.getGoodsType())){
				if ((now-boughtTime)/(1000*60*60)<12){
					if (!"7".equals(member.getRemarks())){
						aj.setSuccess(false);
						aj.setMsg("请您于购买完成12小时后再出售");
						return aj;
					}
				}
			}
			if ("2".equals(goods.getGoodsType())){
				if ((now-boughtTime)/(1000*60*60)<12){
					if (!"7".equals(member.getRemarks())){
						aj.setSuccess(false);
						aj.setMsg("请您于购买完成12小时后再出售");
						return aj;
					}
				}
			}
//			if (price>1000&&price<=2000){
//				if ((now-boughtTime)/(1000*60*60)<23){
//					if (!"7".equals(member.getRemarks())){
//						aj.setSuccess(false);
//						aj.setMsg("请您于购买完成24小时后再出售");
//						return aj;
//					}
//				}
//			}
//			if (price>2000&&price<=3000){
//				if ((now-boughtTime)/(1000*60*60)<23){
//					if (!"7".equals(member.getRemarks())){
//						aj.setSuccess(false);
//						aj.setMsg("请您于购买完成24小时后再出售");
//						return aj;
//					}
//				}
//			}
//			if (price>3000){
//				if ((now-boughtTime)/(1000*60*60)<23){
//					if (!"7".equals(member.getRemarks())){
//						aj.setSuccess(false);
//						aj.setMsg("请您于购买完成24小时后再出售");
//						return aj;
//					}
//				}
//			}
			//11-9 添加限制   抢单期间不可出售
			try {
				SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
				Date nowTime =df.parse(df.format(new Date()));
				//新人区规定时间
				Date beginTime = df.parse("08:00");
				Date endTime = df.parse("9:30");
				//入门区规定时间
				Date beginTime1 = df.parse("13:30");
				Date endTime1 = df.parse("15:00");
				//中级区规定时间
				Date beginTime2 = df.parse("19:00");
				Date endTime2 = df.parse("20:30");
				//调用判断方法是否在规定时间段内
				boolean isTime = DateUtils.timeCalendar(nowTime, beginTime, endTime);
				boolean isTime1 = DateUtils.timeCalendar(nowTime, beginTime1, endTime1);
				boolean isTime2 = DateUtils.timeCalendar(nowTime, beginTime2, endTime2);
				if (isTime || isTime1 || isTime2){
					if (!"7".equals(member.getRemarks())){
						aj.setSuccess(false);
						aj.setMsg("抢单前一小时及抢单期间无法出售，请您于其他时间出售！");
						return aj;
					}
				}
			}catch (ParseException e){
				e.printStackTrace();
			}

			//支付宝方式收款
			if ("0".equals(payType)){
				Userpay userpay = new Userpay();
				userpay.setMember(member);
				List<Userpay> userpayList = userpayMapper.findList(userpay);
				if (userpayList.size()<=0){
					aj.setSuccess(false);
					aj.setMsg("请上传支付宝收款信息");
					return aj;
				}
			}else if ("1".equals(payType)){
				Userwx userwx = new Userwx();
				userwx.setMember(member);
				List<Userwx> userwxList = userwxMapper.findList(userwx);
				if (userwxList.size()<=0){
					aj.setSuccess(false);
					aj.setMsg("请上传微信收款信息");
					return aj;
				}
			}else if ("2".equals(payType)){
				Userbank userbank = new Userbank();
				userbank.setMember(member);
				List<Userbank> userbankList = userbankMapper.findList(userbank);
				if (userbankList.size()<=0){
					aj.setSuccess(false);
					aj.setMsg("请填写银行卡信息");
					return aj;
				}
			}
			//将当前订单改为   0  已托管出售
			ordersList.get(0).setIsSale("0");
//				ordersList.get(0).setDelFlag("1");
			save(ordersList.get(0));
		}
			//生成新出售订单
			SnowFlake snowFlake = new SnowFlake(2, 3);
			//生成订单编号
			Long orderFormNumber = snowFlake.nextId();
			Orders orders1 = new Orders();
			orders1.setOrdersNo(orderFormNumber.toString());
			//0 上架中
			orders1.setOrdersStatus("0");
			//出售人
			orders1.setMember(member);
			if ("7".equals(member.getRemarks())){
				orders1.setRemarks("7");
			}
			//收款方式
			orders1.setPayType(payType);
			//银行卡的话订单存账户姓名和银行卡号
			if ("2".equals(payType)){
				Userbank userbank = new Userbank();
				userbank.setMember(member);
				List<Userbank> userbankList = userbankMapper.findList(userbank);
				if (userbankList.size()>0){
					orders1.setAccountNo(userbankList.get(0).getBankno());
					orders1.setAccountName(userbankList.get(0).getName());
				}
			}
			//商品id
			orders1.setGoods(goods);
			save(orders1);
			//给商品更改状态并该购买价格
			goods.setGoodsStatus("1");

			//根据售卖价格，更改分区
			if (Double.valueOf(salePrice)>0 && Double.valueOf(salePrice)<=800){
				goods.setGoodsType("0");
			}else if (Double.valueOf(salePrice)>800 && Double.valueOf(salePrice)<=1800){
				goods.setGoodsType("1");
			} else if (Double.valueOf(salePrice)>1800 && Double.valueOf(salePrice)<=3000){
				goods.setGoodsType("2");
			}
//			else if (Double.valueOf(salePrice)>2000 && Double.valueOf(salePrice)<=3000){
//				goods.setGoodsType("3");
//			}else if (Double.valueOf(salePrice)>3000 && Double.valueOf(salePrice)<=5000){
//				goods.setGoodsType("4");
//			}else if (Double.valueOf(salePrice)>5000){
//				goods.setGoodsType("5");
//			}
			goods.setPrice(Double.valueOf(salePrice));
			goods.setOrders(orders1);
			//todo 持有人
			goods.setHostUser(member.getMobile());
			goods.preUpdate();
			goodsMapper.update(goods);
			//插入新的商品交易记录
			Trade trade1 = new Trade();
			trade1.setId(IdGen.uuid());
			trade1.setOrderNo(orders1.getOrdersNo());
			trade1.setTradeStatus("0");
			trade1.setMember(member);
			trade1.setGoods(goods);
			trade1.setPayType(payType);
			trade1.setIsNewRecord(true);
			trade1.preInsert();
			tradeMapper.insert(trade1);
//插入新的商品交易记录
		Trades trades1 = new Trades();
		trades1.setId(IdGen.uuid());
		trades1.setOrderNo(orders1.getOrdersNo());
		trades1.setTradeStatus("0");
		trades1.setMember(member);
		trades1.setGoods(goods);
		trades1.setPayType(payType);
		trades1.setIsNewRecord(true);
		trades1.preInsert();
		tradesMapper.insert(trades1);
			//扣除用户钱包   手续费金额
//			BigDecimal money = BigDecimal.valueOf(accountList.get(0).getYue()).subtract(BigDecimal.valueOf(Double.valueOf(charge)));
//			accountList.get(0).setYue(money.doubleValue());
//			accountList.get(0).preUpdate();
//			accountMapper.update(accountList.get(0));

		aj.setSuccess(true);
		aj.setMsg("出售成功");
		return aj;
	}

	/**
	 * 释放未交易完成的订单
	 * @param ids
	 */
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	public void releaseBusinessOrder(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			Orders orders = get(id);
			Goods goods = goodsMapper.get(orders.getGoods().getId());
			//如果是用户上架订单
			if (StringUtils.isNotBlank(goods.getOrders().getId())){
				if (!"3".equals(orders.getOrdersStatus()) && !"0".equals(orders.getOrdersStatus())){
					//删除原订单
					orders.setDelFlag("1");
					save(orders);
					//生成新的上架中的订单
					String orderId = IdGen.uuid();
					Orders newEntity = new Orders();
					newEntity.setId(orderId);

					Orders orders1 = new Orders();
					orders1.setId(orderId);
					orders1.setOrdersNo(orders.getOrdersNo());
					orders1.setOrdersStatus("0");
					orders1.setGoods(orders.getGoods());
					orders1.setMember(orders.getMember());
					orders1.setAccountNo(orders.getAccountNo());
					orders1.setAccountName(orders.getAccountName());
					orders1.setPayType(orders.getPayType());
					orders1.setIsSale("1");
					orders1.setIsNewRecord(true);
					orders1.preInsert();
					save(orders1);
					//商品改回上架中
					goods.setGoodsStatus("1");
					goods.setOrders(newEntity);
					goods.preUpdate();
					goodsMapper.update(goods);
					//删除原交易记录
					Trade trade = new Trade();
					trade.setOrderNo(orders.getOrdersNo());
					List<Trade> tradeList = tradeMapper.findList(trade);
					if (tradeList.size()>0){
						tradeList.get(0).setDelFlag("1");
						tradeList.get(0).preUpdate();
						tradeMapper.update(tradeList.get(0));
						//生成对应的新的交易记录
						Trade trade1 = new Trade();
						trade1.setId(IdGen.uuid());
						trade1.setOrderNo(tradeList.get(0).getOrderNo());
						trade1.setPayType(tradeList.get(0).getPayType());
						trade1.setGoods(tradeList.get(0).getGoods());
						trade1.setMember(tradeList.get(0).getMember());
						trade1.setTradeStatus("0");
						trade1.preInsert();
						tradeMapper.insert(trade1);
					}
					Trades trades = new Trades();
					trades.setOrderNo(orders.getOrdersNo());
					List<Trades> tradesList = tradesMapper.findList(trades);
					if (tradesList.size()>0){
						tradesList.get(0).setDelFlag("1");
						tradesList.get(0).preUpdate();
						tradesMapper.update(tradesList.get(0));
						//生成对应的新的交易记录
						Trades trades1 = new Trades();
						trades1.setId(IdGen.uuid());
						trades1.setOrderNo(tradesList.get(0).getOrderNo());
						trades1.setPayType(tradesList.get(0).getPayType());
						trades1.setGoods(tradesList.get(0).getGoods());
						trades1.setMember(tradesList.get(0).getMember());
						trades1.setTradeStatus("0");
						trades1.preInsert();
						tradesMapper.insert(trades1);
					}
				}
			}else {
				if (!"3".equals(orders.getOrdersStatus()) && !"0".equals(orders.getOrdersStatus())){
					//商品改回上架中
					goods.setGoodsStatus("1");
					goods.preUpdate();
					goodsMapper.update(goods);
					//平台上架   订单直接删除
					orders.setDelFlag("1");
					save(orders);
					//删除原交易记录
					Trade trade = new Trade();
					trade.setGoods(goods);
					List<Trade> tradeList = tradeMapper.findList(trade);
					if (tradeList.size()>0){
						tradeList.get(0).setDelFlag("1");
						tradeList.get(0).preUpdate();
						tradeMapper.update(tradeList.get(0));
						//生成对应的新的交易记录
						Trade trade1 = new Trade();
						trade1.setId(IdGen.uuid());
						trade1.setOrderNo(tradeList.get(0).getOrderNo());
						trade1.setPayType(tradeList.get(0).getPayType());
						trade1.setGoods(tradeList.get(0).getGoods());
						trade1.setMember(tradeList.get(0).getMember());
						trade1.setTradeStatus("0");
						trade1.preInsert();
						tradeMapper.insert(trade1);
					}
					//删除原交易记录
					Trades trades = new Trades();
					trades.setGoods(goods);
					List<Trades> tradesList = tradesMapper.findList(trades);
					if (tradesList.size()>0){
						tradesList.get(0).setDelFlag("1");
						tradesList.get(0).preUpdate();
						tradesMapper.update(tradesList.get(0));
						//生成对应的新的交易记录
						Trades trades1 = new Trades();
						trades1.setId(IdGen.uuid());
						trades1.setOrderNo(tradesList.get(0).getOrderNo());
						trades1.setPayType(tradesList.get(0).getPayType());
						trades1.setGoods(tradesList.get(0).getGoods());
						trades1.setMember(tradesList.get(0).getMember());
						trades1.setTradeStatus("0");
						trades1.preInsert();
						tradesMapper.insert(trades1);
					}
				}
			}

		}
	}


	public List<Orders> findTeamList(Orders orders) {
		return mapper.findTeamList(orders);
	}

	public Page<Orders> findPageForGoods(Page<Orders> ordersPage, Orders orders) {
		return super.findPage(ordersPage, orders);
	}

	@Transactional(readOnly = false)
	public AjaxJson qianggouGoods(Member member,String goodsType) {
		AjaxJson aj = new AjaxJson();
		Goods goods = new Goods();
		goods.setGoodsType(goodsType);
		goods.setGoodsStatus("1");
		List<Goods> goodsList = goodsMapper.findList(goods);
		List<Goods> selfGoods = new ArrayList<>();
		if (goodsList.size()>0){
			for (Goods goods1 : goodsList) {
				Jedis jedis = new Jedis("localhost",6379);
				long count = jedis.incrBy(goods1.getId(),1);
				if (count == 1) {
					//设置有效期10秒
					jedis.expire(goods1.getId(),50);
				}
				if (count > 1) {
					continue;
				}
				//查询当前购买商品是否是再售出
				if (StringUtils.isNotBlank(goods1.getOrders().getId())){
					//判断此商品出售人是否为自己
					Orders boughtOrder = get(goods1.getOrders().getId());
					if (member.getId().equals(boughtOrder.getMember().getId())){
						selfGoods.add(goods1);
						if (selfGoods.size() == goodsList.size()){
							aj.setSuccess(false);
							aj.setMsg("商品抢光了");
							return aj;
						}
						continue;
					}
				}
				Orders entity1 = get(goods1.getOrders().getId());
				SnowFlake snowFlake = new SnowFlake(2, 3);
				//生成订单编号
				Long orderFormNumber = snowFlake.nextId();
				//用户的钱包
				Account account = new Account();
				account.setMember(member);
				List<Account> accountList = accountMapper.findList(account);
				if (accountList.size()>0){
					//查询用户预约记录
					Yuyue yuyue = new Yuyue();
					yuyue.setMember(member);
					yuyue.setZone(goods1.getGoodsType());
//			yuyue.setIsBack("0");
					yuyue.setIsOver("0");
					List<Yuyue> yuyueList = yuyueService.findList(yuyue);
					if (yuyueList.size()>0){
						//应交手续费
						BigDecimal money = BigDecimal.valueOf(goods1.getPrice()).multiply(BigDecimal.valueOf(0.07));
//						if ("2".equals(goods1.getGoodsType())){
//							money = BigDecimal.valueOf(goods1.getPrice()).multiply(BigDecimal.valueOf(0.1));
//						}
						//预约手续费
						BigDecimal pay = BigDecimal.valueOf(yuyueList.get(0).getMoney());
						//如果预约手续费交的多了,返还到余额
						if (pay.compareTo(money) == 1){
							accountList.get(0).setYue(pay.subtract(money).add(BigDecimal.valueOf(accountList.get(0).getYue())).doubleValue());
							accountList.get(0).preUpdate();
							accountMapper.update(accountList.get(0));
							// 返还金额记录
							yuyueList.get(0).setIsOver("1");
							yuyueList.get(0).setIsBack("1");
							yuyueList.get(0).setGiveMoney(pay.subtract(money).doubleValue());
							yuyueService.save(yuyueList.get(0));
							//插入手续费记录
							TeamService teamService = new TeamService();
							teamService.setMember(member);
							teamService.setMoney(money.doubleValue());
							teamService.setGoods(goods1);
							if (StringUtils.isNotBlank(goods1.getOrders().getId())){
								teamService.setNumber(entity1.getOrdersNo());
							}else {
								teamService.setNumber(orderFormNumber.toString());
							}

							teamService.setGoodsprice(goods1.getPrice());
							teamService.setGoodstype(goods1.getGoodsType());
							teamService.setGoodsname(goods1.getName());
							if ("7".equals(member.getRemarks())){
								teamService.setRemarks("7");
							}
							teamServiceService.save(teamService);
						}else {
							//预约记录出局
							yuyueList.get(0).setIsOver("1");
							yuyueService.save(yuyueList.get(0));
							//插入手续费记录
							TeamService teamService = new TeamService();
							teamService.setMember(member);
							teamService.setMoney(money.doubleValue());
							teamService.setGoods(goods1);
							if (StringUtils.isNotBlank(goods1.getOrders().getId())){
								teamService.setNumber(entity1.getOrdersNo());
							}else {
								teamService.setNumber(orderFormNumber.toString());
							}

							teamService.setGoodsprice(goods1.getPrice());
							teamService.setGoodstype(goods1.getGoodsType());
							teamService.setGoodsname(goods1.getName());
							if ("7".equals(member.getRemarks())){
								teamService.setRemarks("7");
							}
							teamServiceService.save(teamService);
						}
					}else {
						//未预约，则扣除未预约的手续费  5%
						BigDecimal money = BigDecimal.valueOf(goods1.getPrice()).multiply(BigDecimal.valueOf(0.07));
//						if ("2".equals(goods1.getGoodsType())){
//							money = BigDecimal.valueOf(goods1.getPrice()).multiply(BigDecimal.valueOf(0.1));
//						}
						//余额不足
						if (money.compareTo(BigDecimal.valueOf(accountList.get(0).getYue())) == 1){
							aj.setSuccess(false);
							aj.setMsg("余额不足,无法交付手续费");
							return aj;
						}
						//扣除手续费
						accountList.get(0).setYue(BigDecimal.valueOf(accountList.get(0).getYue()).subtract(money).doubleValue());
						accountList.get(0).preUpdate();
						accountMapper.update(accountList.get(0));
						//插入手续费记录
						TeamService teamService = new TeamService();
						teamService.setMember(member);
						teamService.setMoney(money.doubleValue());
						teamService.setGoods(goods1);
						if (StringUtils.isNotBlank(goods1.getOrders().getId())){
							teamService.setNumber(entity1.getOrdersNo());
						}else {
							teamService.setNumber(orderFormNumber.toString());
						}

						teamService.setGoodsprice(goods1.getPrice());
						teamService.setGoodstype(goods1.getGoodsType());
						teamService.setGoodsname(goods1.getName());
						if ("7".equals(member.getRemarks())){
							teamService.setRemarks("7");
						}
						teamServiceService.save(teamService);
					}
				}else {
					aj.setSuccess(false);
					aj.setMsg("用户钱包信息有误");
					return aj;
				}

				Orders orders = new Orders();
				//返回定单编号给前端
				String orderNo = "";
				//查询当前购买商品是否是再售出
				if (StringUtils.isNotBlank(goods1.getOrders().getId())){
					//是再出售，更改此订单状态为   1  等待付款
					Orders entity = get(goods1.getOrders().getId());
					if (entity != null){
						orderNo = entity.getOrdersNo();
						entity.setOrdersStatus("1");
						entity.setTomem(member);
						entity.setIsSale("1");
						entity.setMoney(goods1.getPrice());
						save(entity);
						//更新交易记录
						Trade trade = new Trade();
						trade.setGoods(goods1);
						trade.setMember(entity.getMember());
						List<Trade> tradeList = tradeMapper.findList(trade);
						if (tradeList.size()>0){
							tradeList.get(0).setTradeStatus("1");
							tradeList.get(0).setOrderNo(entity.getOrdersNo());
							tradeList.get(0).setPlaceDate(new Date());
							tradeList.get(0).setTomem(member);
							tradeList.get(0).preUpdate();
							tradeMapper.update(tradeList.get(0));
						}
						//更新交易记录
						Trades trades = new Trades();
						trades.setGoods(goods1);
						trades.setMember(entity.getMember());
						List<Trades> tradesList = tradesMapper.findList(trades);
						if (tradesList.size()>0){
							tradesList.get(0).setTradeStatus("1");
							tradesList.get(0).setOrderNo(entity.getOrdersNo());
							tradesList.get(0).setPlaceDate(new Date());
							tradesList.get(0).setTomem(member);
							tradesList.get(0).preUpdate();
							tradesMapper.update(tradesList.get(0));
						}
					}
				}else {
					//首次从平台售出商品，生成订单  1 待付款
					orders.setOrdersNo(orderFormNumber.toString());
					orders.setOrdersStatus("1");
					orders.setGoods(goods1);
					orders.setTomem(member);
					//平台收款方式
					if (goods1.getPlatformCash() != null){
						if (goods1.getPlatformCash().getCashName().contains("银行卡")){
							orders.setPayType("2");
						}else if (goods1.getPlatformCash().getCashName().contains("支付宝")){
							orders.setPayType("0");
						}else if (goods1.getPlatformCash().getCashName().contains("微信")){
							orders.setPayType("1");
						}
					}else {
						orders.setPayType("2");
					}
					orders.setIsSale("1");
					orders.setMoney(goods1.getPrice());
					orders.setRemarks("平台收款");
					if ("7".equals(member.getRemarks())){
						orders.setRemarks("7");
					}
					save(orders);
					orderNo = orders.getOrdersNo();
					//更新交易记录
					Member member1 = new Member();
					member1.setLoginName("admin");
					List<Member> memberList = memberMapper.findList(member1);
					Trade trade = new Trade();
					trade.setGoods(goods1);
					trade.setMember(memberList.get(0));
					List<Trade> tradeList = tradeMapper.findList(trade);
					if (tradeList.size()>0){
						tradeList.get(0).setTradeStatus("1");
						tradeList.get(0).setOrderNo(orderFormNumber.toString());
						tradeList.get(0).setPlaceDate(new Date());
						tradeList.get(0).setTomem(member);
						tradeList.get(0).preUpdate();
						tradeMapper.update(tradeList.get(0));
					}
					Trades trades = new Trades();
					trades.setGoods(goods1);
					trades.setMember(memberList.get(0));
					List<Trades> tradesList = tradesMapper.findList(trades);
					if (tradesList.size()>0){
						tradesList.get(0).setTradeStatus("1");
						tradesList.get(0).setOrderNo(orderFormNumber.toString());
						tradesList.get(0).setPlaceDate(new Date());
						tradesList.get(0).setTomem(member);
						tradesList.get(0).preUpdate();
						tradesMapper.update(tradesList.get(0));
					}
				}
				//订单生成成功后，更改商品状态  2   等待付款
				goods1.setGoodsStatus("2");
				goods1.preUpdate();
				goodsMapper.update(goods1);
				aj.setMsg("恭喜抢购成功");
				aj.setSuccess(true);
				aj.put("orderNo",orderNo);
				System.out.println("orderNo"+orderNo);
				break;
			}
		}else {
			aj.setSuccess(false);
			aj.setMsg("商品抢光了");
		}
		return aj;
	}


}